package com.haochang.chunkvod.controller.adapter.order;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import com.haochang.base.BaseTextView;
import com.haochang.base.EventProxy;
import com.haochang.base.IEventConstant;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.VodConstant;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.database.order.GoodsInfoDao;
import com.haochang.chunkvod.app.utils.SaleUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.app.utils.VodDialog;
import com.haochang.chunkvod.controller.activity.order.EventDetailsActivity;
import com.haochang.chunkvod.controller.activity.order.PackageDetailsActivity;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.controller.fragment.order.OrderFragment;
import com.haochang.chunkvod.model.order.GoodsInfo;
import com.haochang.chunkvod.view.vod.activity.ActivityDialogEnum;
import com.haochang.chunkvod.view.vod.activity.OrderDialogActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;

/**
 * 点餐适配器
 */
public class OrderAdapter extends QuickAdapter
{
	private OrderFragment orderFragment;
	private Context mContext;
	private boolean is_give = false;//是否为增送模式
	private boolean isTakeOut = false;
	/**
	 * 是否为增送类别
	 */
	private boolean is_present = false;
	private String charging_room_type_id;

	public OrderAdapter(OrderFragment orderFragment, Context context, int layoutResId, List data, boolean isTakeOut)
	{
		super(context, layoutResId, data);
		mContext = context;
		this.isTakeOut = isTakeOut;
		this.orderFragment = orderFragment;
	}

	/**
	 * 设置计费房型id
	 *
	 * @param charging_room_type_id 计费房型id
	 */
	public void setChargingRoomTypeId(String charging_room_type_id)
	{
		this.charging_room_type_id = charging_room_type_id;
	}

	/**
	 * 设置增送类别
	 *
	 * @param is_present 是否为增送
	 */
	public void setIsPresent(boolean is_present)
	{
		this.is_present = is_present;
	}

	/**
	 * 设置增送模式
	 *
	 * @param flag  增送模式是否选中
	 */
	public void setChecked(boolean flag)
	{
		is_give = flag;
		notifyDataSetChanged();
	}

	@Override
	protected void convert(final BaseAdapterHelper helper, final Object object)
	{
		final GoodsInfo item = (GoodsInfo) object;
		helper.setText(R.id.tv_foodName, item.getGoodsName());
		helper.setText(R.id.tv_price, String.format("￥%s/" + item.getUnit(), SaleUtils.getSaleString(item.getPrice())));
		BaseTextView tv_oldPrice = helper.getView(R.id.tv_price_gray);
		if (item.getGoods_type() == VodConstant.RETAIL_TYPE)
		{
			if (item.getOriginalPrice() != item.getPrice())//如果现价不等于门店价 且原价>0
			{
				tv_oldPrice.setVisibility(View.VISIBLE);
				tv_oldPrice.setText(String.format("￥%s/" + item.getUnit(), SaleUtils.getSaleString(item.getOriginalPrice())));
				tv_oldPrice.getPaint().setAntiAlias(true);//抗锯齿
				tv_oldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
			} else
				tv_oldPrice.setVisibility(View.GONE);
		} else
			tv_oldPrice.setVisibility(View.GONE);
		if (item.getOrderNum() <= 0)
		{
			helper.getView(R.id.lin_already_order).setVisibility(View.GONE);
		} else
		{
			helper.setText(R.id.tv_orderNum, item.getOrderNum() + "");
			helper.getView(R.id.lin_already_order).setVisibility(View.VISIBLE);
		}
		if (item.getSurplus() <= 0 && item.getGoods_type() != VodConstant.ACTIVE_TYPE)
		{
			helper.getView(R.id.img_cart).setVisibility(View.GONE);
			helper.getView(R.id.tv_estimate).setVisibility(View.VISIBLE);
			helper.getView(R.id.img_give).setVisibility(View.GONE);
			helper.getView(R.id.lin_right).setClickable(false);
			helper.getView(R.id.lin_right).setEnabled(false);
		} else
		{
			if (is_give || is_present)
			{
				helper.getView(R.id.img_cart).setVisibility(View.GONE);
				helper.getView(R.id.tv_estimate).setVisibility(View.GONE);
				helper.getView(R.id.img_give).setVisibility(View.VISIBLE);
			} else
			{
				helper.getView(R.id.img_cart).setVisibility(View.VISIBLE);
				helper.getView(R.id.tv_estimate).setVisibility(View.GONE);
				helper.getView(R.id.img_give).setVisibility(View.GONE);
			}
			helper.getView(R.id.lin_right).setEnabled(true);
			helper.getView(R.id.lin_right).setClickable(true);
			helper.getView(R.id.lin_right).setTag(item);
			helper.getView(R.id.lin_right).setOnClickListener(onBaseClickListener);
		}
		helper.getView(R.id.lay_del).setTag(item);
		helper.getView(R.id.lay_del).setOnClickListener(onBaseClickListener);
		helper.getView(R.id.item_layout).setTag(R.id.order_list, item);
		helper.getView(R.id.item_layout).setOnClickListener(onBaseClickListener);
		if (TextUtils.isEmpty(item.getGoodsId()))
		{
			helper.getView(R.id.tv_foodName).setVisibility(View.INVISIBLE);
			helper.getView(R.id.tv_price).setVisibility(View.INVISIBLE);
			helper.getView(R.id.lin_right).setVisibility(View.INVISIBLE);
			helper.getView(R.id.line).setVisibility(View.GONE);
			helper.getView().setBackgroundColor(mContext.getResources().getColor(R.color.white));
		} else
		{
			helper.getView(R.id.tv_foodName).setVisibility(View.VISIBLE);
			helper.getView(R.id.tv_price).setVisibility(View.VISIBLE);
			helper.getView(R.id.lin_right).setVisibility(View.VISIBLE);
			helper.getView(R.id.line).setVisibility(View.VISIBLE);
			if (is_give || is_present)
				helper.getView(R.id.item_layout).setBackgroundColor(mContext.getResources().getColor(R.color.color_fdf7e6));
			else
				helper.getView(R.id.item_layout).setBackgroundColor(mContext.getResources().getColor(R.color.color_f9f9f9));
		}
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.lay_del:
				final GoodsInfo goodsInfo = (GoodsInfo) v.getTag();
				new VodDialog.Builder(context).dialogEnum(VodDialog.DialogEnum.DOUBLE).btnNegativeText(R.string.cancel).btnNeutralText(R.string.sure)
						.cancelable(false).contentName("确认要从购物车删除该商品吗?").onCustomDialogListener(new VodDialog.Builder.OnCustomDialogListener()
						{
							@Override
							public void onOnlyCancelClick()
							{

							}

							@Override
							public void onOkClick()
							{
								GoodsInfoDao.getDao().deleteBuilder(GoodsInfo.class, "bill_id", goodsInfo.getBillId(), "goods_id", goodsInfo.getGoodsId(),
										"menu_id", goodsInfo.getMenuId(), "is_give", goodsInfo.is_give(), "is_present", goodsInfo.is_present());
								goodsInfo.setOrderNum(0);
								EventProxy.notifyEvent(IEventConstant.EVENT_SHOP_CART);
							}

							@Override
							public void onCancelClick()
							{

							}
						}).build().show();
				break;
			case R.id.lin_right:
				onClickShopOrGive((GoodsInfo) v.getTag());
				break;
			case R.id.item_layout:
				final GoodsInfo item = (GoodsInfo) v.getTag(R.id.order_list);
				if (TextUtils.isEmpty(item.getGoodsId()))
					return;
				if (is_give)
					item.setIs_give(true);
				else
					item.setIs_give(false);
				if (is_present)
					item.setIs_present(true);
				else
					item.setIs_present(false);
				if (item.getGoods_type() == VodConstant.ACTIVE_TYPE)
				{ //活动详情
					item.setCharging_room_type_id(charging_room_type_id);//计费房型id，活动才有
					Intent intent = new Intent(mContext, EventDetailsActivity.class);
					intent.putExtra(IntentKey.GOODSINFO, item);
					mContext.startActivity(intent);
				} else
				{
					if (item.getSurplus() > 0)
					{
						if (item.getGoods_type() == VodConstant.BUNDLE_TYPE)
						{
							Intent intent = new Intent(mContext, PackageDetailsActivity.class);
							intent.putExtra(IntentKey.ORDER_TAKEOUT, isTakeOut);
							intent.putExtra(IntentKey.GOODSINFO, item);
							mContext.startActivity(intent);
						} else if (!TextUtils.isEmpty(item.getStandard_types()) && TextUtils.isEmpty(item.getTaste_types()))
						{
							Intent intent = new Intent(mContext, OrderDialogActivity.class);
							intent.putExtra(IntentKey.GOODSINFO, item);
							intent.putExtra(IntentKey.ORDER_TAKEOUT, isTakeOut);//是否是外带
							intent.putExtra(IntentKey.ACTIVITY_ENUM, ActivityDialogEnum.SIZE);
							intent.putExtra(IntentKey.CUSTOM_DIALOG_LEFT_NAME, "数量");
							orderFragment.startActivityForResult(intent, IntentCode.FLAVOR_REQUEST);
						} else
						{
							boolean isSurplus = GoodsInfoDao.getDao().updateOrCreateFoodNum(item);
							if (!isSurplus)
							{
								ToastUtils.showText(context, item.getGoodsName() + "仅剩" + item.getSurplus() + item.getUnit());
								return;
							}
							EventProxy.notifyEvent(IEventConstant.EVENT_SHOP_CART);
						}
					} else
					{
						ToastUtils.showText(context, item.getGoodsName() + "仅剩" + item.getSurplus() + item.getUnit());
					}
				}
				break;
			default:
				break;
			}
		}
	};

	private void onClickShopOrGive(GoodsInfo item)
	{
		if (TextUtils.isEmpty(item.getGoodsId()))
			return;
		if (is_give)
			item.setIs_give(true);
		else
			item.setIs_give(false);
		if (is_present)
			item.setIs_present(true);
		else
			item.setIs_present(false);
		String flavor = item.getTaste_types();
		String standard = item.getStandard_types();
		ActivityDialogEnum activityDialogEnum = null;
		JSONArray array = null;
		try
		{
			if (!TextUtils.isEmpty(flavor) && TextUtils.isEmpty(standard))
			{
				activityDialogEnum = ActivityDialogEnum.FLAVOR;
				array = new JSONArray(flavor);
			} else if (TextUtils.isEmpty(flavor) && !TextUtils.isEmpty(standard))
			{
				activityDialogEnum = ActivityDialogEnum.SIZE;
				array = new JSONArray(standard);
			} else if (!TextUtils.isEmpty(flavor) && !TextUtils.isEmpty(standard))
			{
				activityDialogEnum = ActivityDialogEnum.FLAVOR;
				array = new JSONArray(flavor);
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		if (array != null && array.length() > 0)
		{
			Intent intent = new Intent(mContext, OrderDialogActivity.class);
			intent.putExtra(IntentKey.GOODSINFO, item);
			intent.putExtra(IntentKey.ORDER_TAKEOUT, isTakeOut);//是否是外带
			intent.putExtra(IntentKey.ACTIVITY_ENUM, activityDialogEnum);
			if (activityDialogEnum == ActivityDialogEnum.SIZE)
				intent.putExtra(IntentKey.CUSTOM_DIALOG_LEFT_NAME, "数量");
			orderFragment.startActivityForResult(intent,
					activityDialogEnum == ActivityDialogEnum.SIZE ? IntentCode.FLAVOR_REQUEST : IntentCode.ORDER_FLAVOR_REQUEST);
		} else
		{
			if (item.getGoods_type() == VodConstant.ACTIVE_TYPE)
				item.setCharging_room_type_id(charging_room_type_id);//计费房型id，活动才有
			Intent intent = new Intent(mContext, OrderDialogActivity.class);
			intent.putExtra(IntentKey.GOODSINFO, item);
			intent.putExtra(IntentKey.ORDER_TAKEOUT, isTakeOut);//是否是外带
			intent.putExtra(IntentKey.ACTIVITY_ENUM, ActivityDialogEnum.DEFAULT);
			intent.putExtra(IntentKey.CUSTOM_DIALOG_LEFT_NAME, "数量");
			orderFragment.startActivityForResult(intent, IntentCode.FLAVOR_REQUEST);
		}
	}

}
